/**
 * 这个类是画布总管类 
 */
function MyCanvas(canvasId){
	try{
		this.canvas = document.getElementById(canvasId) ;
		this.ctx = document.getElementById(canvasId).getContext('2d') ;
		this.isDown = false ;
		this.startPos = new Object() ;
		this.objectArray = new Array() ;
		this.$window = $("#"+canvasId) ;
		this.mode = 'line' ;//默认是线条模式
		this.tmpObj ;
		this.indexOperator ;
		var self = this ;	
			
		$("#"+canvasId).bind("mouseup" , function(e){
			self.mouseUp(e) ;
		}) ;
		$("#"+canvasId).bind("mousedown" , function(e){
			self.mouseDown(e) ;
		}) ;
		$("#"+canvasId).bind("mousemove" , function(e){
			self.mouseMove(e) ;
		}) ;
	}catch(e){alert(e.message)}
}
MyCanvas.prototype.setMode=function(strMode){
	this.mode = strMode ;
}
MyCanvas.prototype.getIndexOper=function(manager){
	/**
	 * 根据不同的设置返回不同的操作对象
	 */
	if(this.mode=='line') return new lineOper(manager) ;
	if(this.mode='rect') return new rectOper(manager) ;
}
//绘制方法
MyCanvas.prototype.paint=function(){
	try{
		this.ctx.beginPath(); 
		this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
		
		for(var i = 0 , size = this.objectArray.length ; i < size ; i ++){
			var tmp = this.objectArray[i] ;
			tmp.paint(this.ctx) ;
		}
		if(this.tmpObj)this.tmpObj.paint(this.ctx) ;
		this.ctx.stroke() ;
		var self = this ;
			
		setInterval(function(){
			self.start() ;
		},1000);
			
		  		
	}catch(e){
		alert(e.message) ;
	}
}
/**
 * 添加节点
 */
MyCanvas.prototype.addNode=function (node){
	this.objectArray.push(node) ;
};
MyCanvas.prototype.mouseDown = function(e){
	try{
		//这里获取操作类别
		this.startPos = this.getEvent(e) ;
		//这里得到当前操作类型
		this.indexOperator = this.getIndexOper(this) ;
		this.indexOperator.mouseDown() ;
	}catch(e){alert(e.message)}
};
MyCanvas.prototype.mouseUp = function(e){
	//根据操作类别调用类别的函数
	this.indexPos = this.getEvent(e) ;
	this.indexOperator.mouseUp() ;
};
MyCanvas.prototype.mouseMove = function(e){
	//根据操作类别调用类别的函数
	this.indexPos = this.getEvent(e) ;
	this.indexOperator.mouseMove() ;
};
MyCanvas.prototype.getEvent = function(e){
	var res = new Object() ;
	
	res.x = e.offsetX ;
	res.y = e.offsetY ;
	return res ;
}